package com.cqyc.imview

import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat


class StatusBarUtils {
    /**
     * View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN： 状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态栏遮住，为避免状态栏遮挡View上的部分信息，需要为View设置fitsSystemWindows=true
     * View.SYSTEM_UI_FLAG_FULLSCREEN： Activity全屏显示时，状态栏被隐藏覆盖掉
     * View.SYSTEM_UI_FLAG_HIDE_NAVIGATION： 导航栏不显示，布局延伸到导航栏
     * View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION： 导航栏显示，布局延伸到导航栏
     * View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR： Android的状态栏的字体颜色默认为白色，只有Android6.0以后才提供了官方的API选择黑色字体
     */
    companion object {
        fun setStatusBar(window: Window) {
            //沉浸
            WindowCompat.setDecorFitsSystemWindows(window, false)
//            val controller = WindowCompat.getInsetsController(window , view)!!
//            controller.hide(WindowInsetsCompat.Type.statusBars())
            //设置专栏栏和导航栏的底色，透明
            window.statusBarColor = Color.TRANSPARENT
            window.navigationBarColor = Color.TRANSPARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                window.navigationBarDividerColor = Color.TRANSPARENT
            }
        }

        fun setStatusBar(window: Window, light: Boolean) {
            //沉浸
            WindowCompat.setDecorFitsSystemWindows(window, false)
            //设置专栏栏和导航栏的底色，透明
            window.statusBarColor = Color.TRANSPARENT
            window.navigationBarColor = Color.TRANSPARENT
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                window.navigationBarDividerColor = Color.TRANSPARENT
            }
            //设置沉浸后专栏栏和导航字体的颜色，
            ViewCompat.getWindowInsetsController(window.findViewById<FrameLayout>(android.R.id.content))
                ?.let { controller ->
                    controller.isAppearanceLightStatusBars = light
                    controller.isAppearanceLightNavigationBars = light
                }
        }

        fun setStatusBarColor(window: Window, @ColorInt color: Int, light: Boolean) {
            WindowCompat.setDecorFitsSystemWindows(window, true)
            window.statusBarColor = color
            window.navigationBarColor = color
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                window.navigationBarDividerColor = color
            }
            //设置沉浸后专栏栏和导航字体的颜色，
            ViewCompat.getWindowInsetsController(window.findViewById<FrameLayout>(android.R.id.content))
                ?.let { controller ->
                    controller.isAppearanceLightStatusBars = light
                    controller.isAppearanceLightNavigationBars = light
                }
        }

        fun setStatusBarColor(window: Window, @ColorInt color: Int, @ColorInt color2: Int, light: Boolean) {
            WindowCompat.setDecorFitsSystemWindows(window, true)
            window.statusBarColor = color
            window.navigationBarColor = color2
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                window.navigationBarDividerColor = color2
            }
            //设置沉浸后专栏栏和导航字体的颜色，
            ViewCompat.getWindowInsetsController(window.findViewById<FrameLayout>(android.R.id.content))
                ?.let { controller ->
                    controller.isAppearanceLightStatusBars = light
                    controller.isAppearanceLightNavigationBars = light
                }
        }
    }
}